package com.oba.king.datalib.sampleproduct;

/**
 * Title:           SampleProductDetailDao
 * Description:
 * Copyright:       Copyright (c) 2003
 * Company:         King Top Limited
 * @author          Wu Liang
 * @version 1.0
 *
 * MODIFICATION HISTORY
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Date Modified        Modified By         Comments
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 26/04/2010           Wu Liang            Code Creation
 */

import java.util.List;

import com.oba.base.dao.BaseDao;
import com.oba.base.interfaces.IHsqlBuilder;
import com.oba.base.util.Util;
import com.oba.king.datalib.productinfo.ProductInfo;

public class SampleProductDetailDao extends BaseDao {
	
	private final static String sqlStr = "SELECT PRODUCT_ID FROM SAMPLEPRODUCTDETAIL";
	
	public IHsqlBuilder buildAll(){
		IHsqlBuilder sql = this.getSqlBuilder("from SampleProductDetail samplePrdt");
		sql.addend("order by samplePrdt.productName");
		return sql;
	} 
	
	public ProductInfo findProductByCode(SampleProductDetail spd) {
		IHsqlBuilder sql = getSqlBuilder("from ProductInfo p");
		sql.add("p.productSerialId = ?" ,spd.getProductCode());
		List<ProductInfo> list = find(sql);
		if(list.size()>0) {
			return list.get(0);
		}
		return new ProductInfo();
	}
	
	/**
	 * 模糊查询
	 * @param productCondition
	 * @return HsqlBuilder sql
//	 */ 
	public IHsqlBuilder srchSamplePrdtCondition(SamplePrdtCondition samplePrdtCondition) {
		IHsqlBuilder sql = getSqlBuilder("from SampleProductDetail samplePrdt");
		sql.add("samplePrdt.productCode =?", samplePrdtCondition.getProductCode().trim());	
		sql.add("samplePrdt.productName like ?", samplePrdtCondition.getProductName(),"%","%");
		if(samplePrdtCondition.getInActive().trim()!=null && !samplePrdtCondition.getInActive().trim().equals("O")){
			sql.add("samplePrdt.inActive =?", samplePrdtCondition.getInActive().trim());	
		}				
		sql.addend("order by samplePrdt.productName");
		return sql;
	}
	
	public IHsqlBuilder srchPrdtForChoose(String productSerialId, String productName){
		//		
		List<Long> ids = jdao.queryForList(sqlStr,Long.class);
		String idsStr = Util.idsFormat(ids);
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		sql.add("prdt.productId not in "+idsStr);
		sql.add("prdt.productSerialId = ?",productSerialId);
		sql.add("prdt.productName like ?",productName ,"%","%");
		sql.addend("order by prdt.productName");
		return sql;
	}
	
	public IHsqlBuilder srchPrdtForChooseAll(){
		String idsStr ="";
		List<Long> ids = jdao.queryForList(sqlStr,Long.class);
		if(ids.size()!=0){
			idsStr = Util.idsFormat(ids);			
		}
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		if(idsStr!=null && !idsStr.trim().equals("")){
			sql.add("prdt.productId not in "+idsStr);
		}		
		sql.addend("order by prdt.productName");
		return sql;
	}		
	
}
